<template>
  <a-card :bordered="false">
    <!-- 查询区域 -->
    <div class="table-page-search-wrapper">
      <a-form layout="inline" @keyup.enter.native="searchQuery">
        <a-row :gutter="24">
          <a-col :lg="3 ">
            <a-form-item label="分类">
              <select-category v-model="queryParam.categoryId"
                               :placeholder="'请选择所属分类'"></select-category>
            </a-form-item>
          </a-col>
          <a-col :lg="3">
            <a-form-item label="品牌">
              <a-select v-model="queryParam.brandId" placeholder="请选择所属品牌">
                <a-select-option :value="item.id" v-for="item in brandList" :key="item.id">
                  {{ item.name }}
                </a-select-option>
              </a-select>
            </a-form-item>
          </a-col>
          <a-col :lg="2">
            <a-form-item label="状态">
              <a-select v-model="queryParam.status" placeholder="请选择状态">
                <a-select-option :value="item.id" v-for="item in statusList" :key="item.id">
                  {{ item.name }}
                </a-select-option>
              </a-select>
            </a-form-item>
          </a-col>
          <a-col :lg="4">
            <a-form-item label="关键字">
              <a-input v-model="queryParam.keywords" placeholder="请输入检索关键字" :allowClear="true"></a-input>
            </a-form-item>
          </a-col>
          <a-col :lg="4">
              <span style="float: left;overflow: hidden;" class="table-page-search-submitButtons">
                  <a-button type="primary" @click="searchQuery" icon="search">查询</a-button>
                  <a-button type="primary" @click="resetSearch" icon="reload" style="margin-left: 8px">重置</a-button>
              </span>
          </a-col>
        </a-row>
      </a-form>
    </div>
    <!-- 查询区域-END -->
    <div>
      <a-table
        ref="table"
        size="middle"
        :scroll="{x:true}"
        bordered
        rowKey="id"
        :columns="columns"
        :dataSource="dataSource"
        :pagination="ipagination"
        :loading="loading"
        class="j-table-force-nowrap"
        @change="handleTableChange">
        <span slot="action" slot-scope="text, record">
          <a-popconfirm title="确认操作么？" ok-text="确认" cancel-text="取消" @confirm="updatePublishStatus(record)">
            <a href="#">{{ record.publishStatus === 1 ? '下架' : '上架' }}</a>
          </a-popconfirm>
          <a-divider type="vertical" />
           <a @click="handleEdit(record)">规格</a>
        </span>
      </a-table>
    </div>
    <spu-modal ref="modalForm" @ok="modalFormOk"></spu-modal>
  </a-card>
</template>

<script>

import '@/assets/less/TableExpand.less'
import { mixinDevice } from '../../utils/mixin'
import { JeecgListMixin } from '../../mixins/JeecgListMixin'
import SpuModal from './spu/SpuModal.vue'
import SelectCategory from '../../components/compents/SelectCategory.vue'
import { getAction, postAction } from '../../api/manage'

export default {
  name: 'PmsSpuInfoList',
  mixins: [JeecgListMixin, mixinDevice],
  components: {
    SelectCategory,
    SpuModal
  },
  data() {
    return {
      description: '管理页面',
      brandList: [],
      statusList: [{ id: 0, name: '新建' }, { id: 1, name: '上架' }, { id: 2, name: '下架' }],
      // 表头
      columns: [
        {
          title: '序号',
          dataIndex: '',
          key: 'rowIndex',
          width: 60,
          align: 'center',
          customRender: function(t, r, index) {
            return parseInt(index) + 1
          }
        },
        {
          title: '商品名称',
          align: 'center',
          dataIndex: 'spuName'
        },
        {
          title: '商品描述',
          align: 'center',
          dataIndex: 'spuDescription'
        },
        {
          title: '所属分类',
          align: 'center',
          dataIndex: 'catalogName'
        },
        {
          title: '品牌',
          align: 'center',
          dataIndex: 'brandName'
        },
        {
          title: '上架状态',
          align: 'center',
          dataIndex: 'publishStatus',
          customRender: function(text) {
            return text === 0 ? '新建' : text === 1 ? '上架' : '下架'
          }
        },
        {
          title: '创建时间',
          align: 'center',
          dataIndex: 'createTime',
          customRender: function(text) {
            return text ? text : '暂无数据'
          }
        },
        {
          title: '修改时间',
          align: 'center',
          dataIndex: 'updateTime',
          customRender: function(text) {
            return text ? text : '暂无数据'
          }
        },
        {
          title: '操作',
          dataIndex: 'action',
          align: 'center',
          fixed: 'right',
          width: 147,
          scopedSlots: { customRender: 'action' }
        }
      ],
      url: {
        list: '/product/spuInfo/list',
        delete: '/product/spuInfo/delete',
        deleteBatch: '/product/spuInfo/deleteBatch',
        initBrandList: '/product/brand/list',
        edit: '/product/spuInfo/edit'
      }
    }
  },
  created() {
    this.initBrandList()
  },
  computed: {},
  methods: {
    /**
     * 初始化品牌列表
     */
    initBrandList() {
      getAction(this.url.initBrandList).then(res => {
        if (res.success) {
          this.brandList = res.result.records
        } else {
          this.$message.error('获取品牌列表失败!')
        }
      })
    },
    /**
     * 重置搜索
     */
    resetSearch() {
      this.queryParam = {}
      this.loadData()
    },
    /**
     * 修改上下架状态
     * @param record
     */
    updatePublishStatus(record) {
      let status = record.publishStatus
      let params = {
        id: record.id
      }
      switch (status) {
        case 1:
          params.publishStatus = 2
          break
        case 2:
        case 0:
          params.publishStatus = 1
          break
        default:
          break
      }
      postAction(this.url.edit, params).then(res => {
        if (res.success) {
          this.$message.success('操作成功！')
          this.loadData()
        } else {
          this.$message.error(res.message)
        }
      })
    }
  }
}
</script>
<style scoped>
@import '/src/assets/less/common.less';
</style>